﻿using QuanLyTiemVang.BUS;
using QuanLyTiemVang.Classes;
using QuanLyTiemVang.DAO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace QuanLyTiemVang.GUI
{
    /// <summary>
    /// Interaction logic for DanhSachThoGiaCong.xaml
    /// </summary>
    public partial class DanhSachThoGiaCong : Window, IPhanQuyenHelper
    {
        public DanhSachThoGiaCong()
        {
            InitializeComponent();
            
        }

        protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
        {
            base.OnMouseLeftButtonDown(e);

            // Begin dragging the window
            this.DragMove();
        }

        private void Refresh()
        {
            txtMaTho.Text = ThoGiaCongDAO.GetLastMaThoGiaCong().ToString();
            txtTenTho.Text = "";
            txtDiaChi.Text = "";
            btnThem.IsEnabled = true;
            btnCapNhat.IsEnabled = false;
        }
        private void btnThoat_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void btnThem_Click(object sender, RoutedEventArgs e)
        {
            if (txtTenTho.Text == "")
            {
                MessageBox.Show("Chưa nhập tên thợ.", "Lỗi", MessageBoxButton.OK, MessageBoxImage.Error);
                txtTenTho.Focus();
            }
            else if (txtDiaChi.Text == "")
            {
                MessageBox.Show("Chưa nhập địa chỉ thợ.", "Lỗi", MessageBoxButton.OK, MessageBoxImage.Error);
                txtDiaChi.Focus();
            }
            else
            {
                THOGIACONG tgc = new THOGIACONG
                {
                    TenTho = txtTenTho.Text,
                    DiaChi = txtDiaChi.Text
                };
                if (ThoGiaCongBUS.ThemThoGiaCong(tgc))
                {
                    Window_Loaded(sender, e);
                    Refresh();
                    MessageBox.Show("Thêm thành công");
                }
                else
                    MessageBox.Show("Thêm thất bại");
            }
        }

        private void btnXoa_Click(object sender, RoutedEventArgs e)
        {
            List<THOGIACONG> lstTho = ThoGiaCongDAO.GetAllThoGiaCong();
            bool isSuccessful = false;

            foreach (THOGIACONG item in lstTho)
            {
                if (item.IsChecked)
                {
                    isSuccessful = true;
                }
            }

            if (isSuccessful == false)
                return;

            MessageBoxResult result = MessageBox.Show("Bạn có chắc chắn muốn xóa các trường đã chọn?", "Xác nhận xóa", MessageBoxButton.YesNo, MessageBoxImage.Question);
            if (result == MessageBoxResult.No)
            {
                return;
            }

            
            foreach (THOGIACONG item in lstTho)
            {
                if (item.IsChecked)
                {
                    if (ThoGiaCongBUS.XoaThoGiaCong(item))
                    {

                        //MessageBox.Show("Xóa thành công");
                    }
                    else
                    {
                        MessageBox.Show("Xoá thất bại");
                        return;
                    }
                }
            }
            Window_Loaded(sender, e);
            Refresh();
            MessageBox.Show("Xóa thành công");
            this.btnXoa.IsEnabled = false;
        }

        //private void btnThemMoi_Click(object sender, RoutedEventArgs e)
        //{
        //    Refresh();
        //}

        private void btnCapNhat_Click(object sender, RoutedEventArgs e)
        {
            if (txtMaTho.Text == "")
                return;

            if (txtTenTho.Text == "")
            {
                MessageBox.Show("Chưa nhập tên thợ.", "Lỗi", MessageBoxButton.OK, MessageBoxImage.Error);
                txtTenTho.Focus();
                return;
            }
            else if (txtDiaChi.Text == "")
            {
                MessageBox.Show("Chưa nhập địa chỉ thợ.", "Lỗi", MessageBoxButton.OK, MessageBoxImage.Error);
                txtDiaChi.Focus();
                return;
            }

            THOGIACONG tgc = new THOGIACONG
            {
                MaTho = int.Parse(txtMaTho.Text),
                TenTho = txtTenTho.Text,
                DiaChi = txtDiaChi.Text
            };
            if (ThoGiaCongBUS.CapNhatThoGiaCong(tgc))
            {
                Window_Loaded(sender, e);
                Refresh();
                MessageBox.Show("Cập nhật thành công");
            }
            else
                MessageBox.Show("Cập nhật thất bại");
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            List<THOGIACONG> dt = ThoGiaCongDAO.GetAllThoGiaCong();
            dgDanhSachTho.ItemsSource = dt;
            this.Refresh();
        }


        private void dgDanhSachTho_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            int index = dgDanhSachTho.SelectedIndex;
            List<THOGIACONG> lstTho = ThoGiaCongDAO.GetAllThoGiaCong();
            if (index < 0 || index >= lstTho.Count)
                return;
            //lstTho[index].IsChecked = !lstTho[index].IsChecked;

            dgDanhSachTho.ItemsSource = lstTho;
            //if (lstTho[index].IsChecked)
            {
                txtMaTho.Text = lstTho[index].MaTho.ToString();
                txtTenTho.Text = lstTho[index].TenTho;
                txtDiaChi.Text = lstTho[index].DiaChi;
            }

            btnThem.IsEnabled = false;
            btnCapNhat.IsEnabled = true;
        }

        private void dgDanhSachTho_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            int index = dgDanhSachTho.SelectedIndex;
            List<THOGIACONG> lstTho = ThoGiaCongDAO.GetAllThoGiaCong();
            if (index < 0 || index >= lstTho.Count)
                return;
            //lstTho[index].IsChecked = !lstTho[index].IsChecked;

            dgDanhSachTho.ItemsSource = lstTho;
            //if (lstTho[index].IsChecked)
            {
                txtMaTho.Text = lstTho[index].MaTho.ToString();
                txtTenTho.Text = lstTho[index].TenTho;
                txtDiaChi.Text = lstTho[index].DiaChi;
            }

            btnThem.IsEnabled = false;
            btnCapNhat.IsEnabled = true;
        }

        void IPhanQuyenHelper.SetAdminFunction()
        {
            btnThem.IsEnabled = true;
            //btnThemMoi.IsEnabled = true;
            btnCapNhat.IsEnabled = true;
            btnXoa.IsEnabled = true;
        }

        void IPhanQuyenHelper.SetStaffFunction()
        {
            btnThem.IsEnabled = false;
            //btnThemMoi.IsEnabled = false;
            btnCapNhat.IsEnabled = true;
            btnXoa.IsEnabled = false;
        }

        void IPhanQuyenHelper.SetCustomerFunction()
        {
            btnThem.IsEnabled = false;
            //btnThemMoi.IsEnabled = false;
            btnCapNhat.IsEnabled = false;
            btnXoa.IsEnabled = false;
        }

        void IPhanQuyenHelper.SetNonFunction()
        {
            this.Close();
        }

        private void btnThemMoi_Click(object sender, RoutedEventArgs e)
        {
            this.Refresh();
        }

        private void txtTenTho_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key != Key.Left && e.Key != Key.Right && e.Key != Key.Space && e.Key != Key.Back && e.Key != Key.OemQuotes && e.Key != Key.Tab && ((e.Key < Key.A) || (e.Key > Key.Z)))
                e.Handled = true;
        }
    }
}
